package cn.ichiva.luckysheet.utils;

import java.lang.annotation.*;
import java.lang.reflect.Field;

/**
 * TODO
 *
 * @author pzq
 * @version 1.0
 * @date 2022/1/11 16:29
 */
public class test123 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1=Class.forName("cn.ichiva.luckysheet.utils.Student2");
    //通过反射获取注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation :annotations) {
//            System.out.println(annotation);
        }
        //获得注解value的值
        Annotation annotation=c1.getAnnotation(Tablekk.class);
        Tablekk tablekk=(Tablekk)c1.getAnnotation(Tablekk.class);
        String value=tablekk.value();
//        System.out.println(value);

        //获得类指定的注解
        Field field= c1.getDeclaredField("name");
        Field[] fields=c1.getDeclaredFields();
        for (Field field1:fields) {
            System.out.println(field1);
        }
        Filedk filedk=field.getAnnotation(Filedk.class);
        System.out.println(filedk.clumnName());
        System.out.println(filedk.legth());
        System.out.println(filedk.type());

    }
}
@Tablekk("db_student")
class Student2 {
    @Filedk(clumnName = "db_id", type = "int", legth = 10)
    private int id;
    @Filedk(clumnName = "db_age", type = "int", legth = 10)
    private int age;
    @Filedk(clumnName = "db_name", type = "varchar", legth = 3)
    private String name;

    public Student2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public Student2() {
    }

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablekk{
    String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Filedk{
    String clumnName();
    String type();
    int legth();
}
